﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
using GeneralLibs;
using DevExpress.XtraCharts;

namespace BI
{
    public partial class UcChart : UserControl
    {
        public UcChart()
        {
            InitializeComponent();
        }

        private void UcChart_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            cmbTaiKhoan.DataSource = DanhMucTaiKhoan.Instance.DataView;
            cmbTaiKhoan.DisplayColumn = "SoHieu";
            cmbTaiKhoan.ValueColumn = "ID";
            cmbTaiKhoan.AddVisibleColumn("SoHieu", "Số hiệu", 100, 1);
            cmbTaiKhoan.AddVisibleColumn("Ten", "Tên", 300, 2);
            cmbTaiKhoan.EditValue = Guid.Empty;

            DataColumn column1 = new DataColumn("Series1Name", System.Type.GetType("System.String"));
            Data.Columns.Add(column1);

            DataColumn column2 = new DataColumn("Series1Value", System.Type.GetType("System.Decimal"));
            Data.Columns.Add(column2);
        }

        public DataTable _Data = new DataTable();
        public DataTable Data
        {
            get
            {
                return _Data;
            }
            set
            {
                _Data = value;
            }
        }

        public void GenChart(string seriesName)
        {
            ChartControl chart = new ChartControl();
            Series series1 = new Series(seriesName, ViewType.Line);
            chart.Series.Add(series1);

            foreach(DataRow row in _Data.Rows)
            {
                series1.Points.Add(new SeriesPoint((string)row["Series1Name"], (decimal)row["Series1Value"]));
            }
            //LineSeriesView view = (LineSeriesView)series1.View;
            //view.
            for (int i =0;i<panelChart.Controls.Count;i++) panelChart.Controls.RemoveAt(0);

            chart.Dock = DockStyle.Fill;
            panelChart.Controls.Add(chart);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            BasicForms.FrmProgressBarDev.StartShowProgress();

            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = Config.TimeOut;

                DateTime dtPointNeedCalculate = dtNgayBatDau.DateTime;

                int total = (int)dtNgayKetThuc.DateTime.Subtract(dtNgayBatDau.DateTime).TotalHours / (int)numInterval.Value;
                int processed = 0;
                _Data.Rows.Clear();
                while (dtPointNeedCalculate <= dtNgayKetThuc.DateTime)
                {
                    SoDuTaiKhoan soDu = Lists.SoDuTaiKhoan.CalculateFromDB(cmd, dtPointNeedCalculate, (Guid)cmbTaiKhoan.EditValue);
                    DataRow row = _Data.NewRow();
                    row["Series1Name"] = string.Format("{0}/{1}/{2}", dtPointNeedCalculate.Day, dtPointNeedCalculate.Month, dtPointNeedCalculate.Year);
                    row["Series1Value"] = soDu.DuNo;
                    _Data.Rows.Add(row);
                    processed++;

                    BasicForms.FrmProgressBarDev.Status = string.Format("{0}/{1}", processed, total);
                    dtPointNeedCalculate = dtPointNeedCalculate.AddHours((double)numInterval.Value);
                }

                GenChart("So du");
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
            }

            BasicForms.FrmProgressBarDev.FinishShowProgress();
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime ngayBatDau = new DateTime(); if (LocalSettings.HeThong.CheDoBiKiemTra) ngayBatDau = DateTime.Now.AddDays(-2); else ngayBatDau = dtNgayBatDau.DateTime; //DateTime ngayBatDau = dtNgayBatDau.DateTime;
                DateTime ngayKetThuc = dtNgayBatDau.DateTime;
                switch (cmbKyBaoCao.Text)
                {
                    case "Hôm nay":
                        dtNgayBatDau.Enabled = true;
                        dtNgayKetThuc.Enabled = true;
                        DateTime now = GeneralLibs.CommonLib.GetSystemDateTime();
                        ngayBatDau = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                        ngayKetThuc = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
                        break;
                    case "Tùy chọn":
                        dtNgayBatDau.Enabled = true;
                        dtNgayKetThuc.Enabled = true;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
                        break;
                    case "Cả năm":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 12, 31, 23, 59, 59);
                        break;
                    case "Quý I":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 3, 31, 23, 59, 59);
                        break;
                    case "Quý II":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 4, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 6, 30, 23, 59, 59);
                        break;
                    case "Quý III":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 7, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 9, 30, 23, 59, 59);
                        break;
                    case "Quý IV":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 10, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 12, 31, 23, 59, 59);
                        break;
                    default:
                        try
                        {
                            dtNgayBatDau.Enabled = false;
                            dtNgayKetThuc.Enabled = false;
                            string strThang = ((string)cmbKyBaoCao.SelectedItem).Substring(((string)(cmbKyBaoCao.SelectedItem)).LastIndexOf(" "));
                            int thang = int.Parse(strThang);

                            ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, thang, 1);
                            ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, thang,
                                            DateTime.DaysInMonth(GeneralLibs.Config.NamTaiChinh, thang), 23, 59, 59);
                        }
                        catch (SqlException sqlEx)
                        {
                            throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                        }
                        catch (Exception ex)
                        {
                            if (ex is CustomException) throw ex;
                            else
                            {
                                throw new Exception(ex.Message);
                            }
                        }
                        break;
                }
                dtNgayBatDau.DateTime = ngayBatDau;
                dtNgayKetThuc.DateTime = ngayKetThuc;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
